# -*- coding: utf-8 -*-
from datetime import datetime

from odoo import api, fields, models
from odoo.tools.float_utils import float_compare, float_round
from odoo.tools.translate import _
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
from odoo.exceptions import UserError

import logging

_logger = logging.getLogger(__name__)


class Quant(models.Model):
    """ Quants are the smallest unit of stock physical instances """
    _inherit = "stock.quant"

    @api.multi
    def _quant_create_from_move(self, qty, move, lot_id=False, owner_id=False,
                                src_package_id=False, dest_package_id=False,
                                force_location_from=False, force_location_to=False):
        # price_unit = move.get_price_unit()
        rounding = move.product_id.uom_id.rounding
        # 目标库位的记录更新
        move.get_self_force_date()
        date_now = datetime.strftime(datetime.now(), DEFAULT_SERVER_DATETIME_FORMAT)
        self.env['stock.inout.line'].sudo().inout_update_create_report(
            product_id=move.product_id,
            location_id=force_location_to and force_location_to or move.location_dest_id,
            qty=float_round(qty, precision_rounding=rounding),
            # cost=price_unit,
            str_in_date=move.force_date and move.force_date or date_now,
            company_id=move.company_id,
            lot_id=lot_id,
            # owner_id=owner_id,
            package_id=dest_package_id
        )
        # 源库位的记录更新
        self.env['stock.inout.line'].sudo().inout_update_create_report(
            product_id=move.product_id,
            location_id=force_location_from and force_location_from or move.location_id,
            qty=float_round(-qty, precision_rounding=rounding),
            # cost=price_unit,
            str_in_date=move.force_date and move.force_date or date_now,
            company_id=move.company_id,
            lot_id=lot_id,
            # owner_id=owner_id,
            package_id=src_package_id
        )

        return super(Quant, self)._quant_create_from_move(qty, move, lot_id=lot_id, owner_id=owner_id,
                                                          src_package_id=src_package_id,
                                                          dest_package_id=dest_package_id,
                                                          force_location_from=force_location_from,
                                                          force_location_to=force_location_to)
        # return self.sudo().create(vals)

    @api.multi
    def _quant_update_from_move(self, move, location_dest_id, dest_package_id, lot_id=False, entire_pack=False):
        for self_quant in self:
            rounding = move.product_id.uom_id.rounding
            qty = self_quant.qty
            # price_unit = self.price_unit
            # owner_id = self.owner_id.id
            # 目标库位的记录更新
            move.get_self_force_date()
            date_now = datetime.strftime(datetime.now(), DEFAULT_SERVER_DATETIME_FORMAT)
            self.env['stock.inout.line'].sudo().inout_update_create_report(
                product_id=move.product_id,
                location_id=location_dest_id,
                qty=float_round(qty, precision_rounding=rounding),
                # cost=price_unit,
                str_in_date=move.force_date and move.force_date or date_now,
                company_id=move.company_id,
                lot_id=lot_id,
                # owner_id=owner_id,
                package_id=dest_package_id
            )
            # 源库位的记录更新
            self.env['stock.inout.line'].sudo().inout_update_create_report(
                product_id=move.product_id,
                location_id=self_quant.location_id,
                qty=float_round(-qty, precision_rounding=rounding),
                # cost=price_unit,
                str_in_date=move.force_date and move.force_date or date_now,
                company_id=move.company_id,
                lot_id=lot_id,
                # owner_id=owner_id,
                package_id=entire_pack
            )

        res = super(Quant, self)._quant_update_from_move(move, location_dest_id, dest_package_id, lot_id=lot_id,
                                                         entire_pack=entire_pack)
